import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

// 加载网络图片的功能
// CachedNetworkImage可以直接使用，也可以通过ImageProvider使用。Image(image: CachedNetworkImageProvider(url))
// 由于aliyun x-oss-process处理png图片时只支持真彩色输出，对于原来为索引色的png图片，oss处理后文件大小会比原图更大
class MyCachedNetworkImage extends StatelessWidget {
  final String imageUrl;

  /// Widget displayed while the target [imageUrl] is loading.
  final PlaceholderWidgetBuilder? placeholder;

  /// Widget displayed while the target [imageUrl] failed loading.
  final LoadingErrorWidgetBuilder? errorWidget;

  /// Optional builder to further customize the display of the image.
  final ImageWidgetBuilder? imageBuilder;

  /// Optional headers for the http request of the image url
  final Map<String, String>? httpHeaders;

  /// If non-null, this color is blended with each image pixel using [colorBlendMode].
  final Color? color;

  final BoxFit? fit;

  final int? memCacheWidth;
  final int? memCacheHeight;
  final String? cacheKey;
  final int? maxWidthDiskCache;
  final int? maxHeightDiskCache;
  final double? width;
  final double? height;
  final Alignment? alignment;

  const MyCachedNetworkImage({
    super.key,
    this.placeholder,
    this.errorWidget,
    this.fit,
    this.imageBuilder,
    this.width,
    this.height,
    this.alignment = Alignment.center,
    this.color,
    this.memCacheWidth,
    this.memCacheHeight,
    this.cacheKey,
    this.maxWidthDiskCache,
    this.maxHeightDiskCache,
    required this.imageUrl,
    this.httpHeaders,
  });

  @override
  Widget build(BuildContext context) {
    return CachedNetworkImage(
      imageUrl: imageUrl,
      imageBuilder: imageBuilder,
      placeholder: placeholder,
      errorWidget: errorWidget,
      width: width,
      height: height,
      fit: fit,
      alignment: alignment!,
      color: color,
      memCacheHeight: memCacheHeight,
      memCacheWidth: memCacheWidth,
      maxHeightDiskCache: maxHeightDiskCache,
      maxWidthDiskCache: maxWidthDiskCache,
      httpHeaders: httpHeaders,
    );
  }
}
